package d5_peoxy02;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyUtil {
    public static UserService createProxy(UserService obj) {
        return (UserService) Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                //代理对象干活的地方
                long start = System.currentTimeMillis();
                //找被代理对象的方法去跑
                Object rs = method.invoke(obj, args);
                //统计结束时间
                long end = System.currentTimeMillis();
                System.out.println(method.getName() + "运行的时间是" + (end - start) / 1000.0 + "s");
                return rs;
            }
        });
    }
}
